package com.carreirati.evaluation.domain;

import java.util.List;
import javax.persistence.TypedQuery;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@RooJavaBean
@RooJpaActiveRecord(identifierType = QuestionParameterPK.class, versionField = "", table = "question_parameter", schema = "dbo")
@RooDbManaged(automaticallyDelete = true)
@RooJson
public class QuestionParameter {

    @Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        QuestionParameterPK pk = new QuestionParameterPK(this.getQuestionId().getId(), this.getParameterId().getId());
        this.setId(pk);
        this.entityManager.persist(this);
    }

    public static long countQuestionParameters() {
        return entityManager().createQuery("SELECT COUNT(*) FROM QuestionParameter o", Long.class).getSingleResult();
    }

    public static List<com.carreirati.evaluation.domain.QuestionParameter> findQuestionParametersByQuestionId(Long questionId) {
        return entityManager().createNativeQuery("SELECT q.* FROM Question_Parameter q inner join parameter p on q.parameter_id =p.id AND q.question_id = '" + questionId + "' ORDER BY p.grupo, p.weight desc", QuestionParameter.class).getResultList();
    }

    public static com.carreirati.evaluation.domain.QuestionParameter findQuestionParameters(Long questionId, Long parameterId) {
        return (QuestionParameter) entityManager().createNativeQuery("SELECT q.* FROM Question_Parameter q WHERE q.question_Id ='" + questionId + "' AND q.parameter_Id = '" + parameterId + "' ", QuestionParameter.class).getSingleResult();
    }

    @Transactional
    public com.carreirati.evaluation.domain.QuestionParameter merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        String sqlString = "UPDATE Question_Parameter SET description = '" + this.getDescription() + "' WHERE question_Id ='" + this.getId().getQuestionId() + "' AND parameter_Id = '" + this.getId().getParameterId() + "'";
        this.entityManager.createNativeQuery(sqlString).executeUpdate();
        this.entityManager.flush();
        return this;
    }
}
